package middleware

import (
	"github.com/gin-gonic/gin"
	"mz_water/entity"
	"mz_water/util"
	"net/http"
)

func Check() gin.HandlerFunc {
	return func(c *gin.Context) {
		// 保存登录态
		p := util.Pool.Get()
		defer p.Close() //函数运行结束 ，把连接放回连接池
		res := entity.Result{}
		token := c.Request.Header.Get("token")
		if len(token) > 0 {
			_, err := p.Do("sismember", "wx_session", token)
			if err != nil {
				res.SetCode(101)
				res.SetMessage("请重新登录")
				c.JSON(http.StatusOK, res)
				c.Abort()
			}
		} else {
			res.SetCode(101)
			res.SetMessage("请重新登录")
			c.JSON(http.StatusOK, res)
			c.Abort()
		}
		c.Next()
	}
}
